package com.lianniu.mgr.cms.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;

import com.lianniu.mgr.cms.model.SpecialArticleVo;
import com.lianniu.mgr.cms.service.ISpecialArticleService;
import com.lianniu.mgr.pagination.PaginationInfo;


@Controller
@RequestMapping("/rest/SpecialArticle")
public class SpecialArticleRestController {
	
private final static Logger logger = Logger.getLogger(SpecialArticleRestController.class);
	
	@Autowired
	private ISpecialArticleService specialArticleService;
	
	@RequestMapping(value="/SpecialArticleList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult SpecialArticleList(HttpServletRequest request, SpecialArticleVo curVo, PaginationInfo paginationInfo)
	 {
	       logger.info("SpecialArticleRestController  SpecialArticleList begin.");
	       try {
	           
	           logger.info("SpecialArticleList begin.");          
	           BaseResult baseResult = specialArticleService.selectBySpecialArticleForPage(curVo, paginationInfo);
	           logger.info("SpecialArticleList end.");
	           logger.info("SpecialArticleRestController SpecialArticleList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("SpecialArticleRestController SpecialArticleList error:" + e.getMessage());
	       logger.error("SpecialArticleRestController SpecialArticleList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	
	
	@RequestMapping(value = {"/SpecialArticleAdd"}, method = RequestMethod.POST)
	public @ResponseBody BaseResult SpecialArticleAdd(HttpServletRequest request,SpecialArticleVo curVo) {
	      logger.info("SpecialArticleRestController SpecialArticleAdd begin.");      
	      logger.info("SpecialArticleRestController SpecialArticleAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("SpecialArticleAdd begin.");
	        
	          BaseResult baseResult = specialArticleService.insertSpecialArticle(curVo);
	          logger.info("SpecialArticleAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("SpecialArticleAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("SpecialArticleAdd error:"+e.getMessage());
	      logger.info("SpecialArticleRestController SpecialArticleAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	
	
	@RequestMapping(value = {"/SpecialArticleEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult SpecialArticleEdit(HttpServletRequest request,SpecialArticleVo curVo) {
	      logger.info("SpecialArticleRestController SpecialArticleEdit begin.");      
	      logger.info("SpecialArticleRestController SpecialArticleEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("SpecialArticleEdit begin.");
	          BaseResult baseResult = specialArticleService.updateById(curVo);
	          logger.info("SpecialArticleEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("SpecialArticleEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("SpecialArticleRestController error:"+e.getMessage());
	      logger.info("SpecialArticleRestController SpecialArticleEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }
	
	@RequestMapping(value="/selectSpecialArticleById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectSpecialArticleById(HttpServletRequest request,SpecialArticleVo curVo) {
	        logger.info("SpecialArticleRestController selectSpecialArticleById begin.");
	        try {	                    
	          BaseResult baseResult = specialArticleService.selectById(curVo);
	          logger.info("SpecialArticleRestController end.");
	          logger.info("SpecialArticleRestController selectSpecialArticleById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("SpecialArticleRestController selectSpecialArticleById error:" + e.getMessage());
	          logger.error("SpecialArticleRestController selectSpecialArticleById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	 
	 @RequestMapping(value = {"/SpecialArticleDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult SpecialArticleDel(HttpServletRequest request) {
	      logger.info("SpecialArticleRestController SpecialArticleDel begin.");
	     try {          
	          logger.info("SpecialArticleDel begin.");
	          BaseResult baseResult = specialArticleService.deleteSpecialArticleBatch(request.getParameter("ids").split(","));		
	          logger.info("SpecialArticleDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("SpecialArticleDel end.");
	          
	          logger.info("SpecialArticleRestController SpecialArticleDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("SpecialArticleRestController SpecialArticleDel error:" + e.getMessage());
	      logger.info("SpecialArticleRestController SpecialArticleDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	 
	 @RequestMapping(value = {"/checkTitle"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkTitle(HttpServletRequest request) {
	      Boolean flag=true;
	      String title=  request.getParameter("title");
	      logger.info("SpecialArticleRestController checkTitle begin.");
	     try {          
	          logger.info("checkTitle begin.");
	          if("".equalsIgnoreCase(title)||title==null)
	          {return flag;}
	          flag = specialArticleService.checkTitle(title);         
	          logger.info("checkTitle end.");
	          
	          logger.info("SpecialArticleRestController checkTitle end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("SpecialArticleRestController checkTitle error:" + e.getMessage());
	      logger.info("SpecialArticleRestController checkTitle end.");
	      return flag;
	    }

	  }
	 
	 @RequestMapping(value = {"/checkTitleEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkTitleEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String title=  request.getParameter("title");
	      String id=request.getParameter("id");
	      logger.info("SpecialArticleRestController checkTitleEdit begin.");
	     try {          
	          logger.info("checkTitleEdit begin.");
	          SpecialArticleVo vo=new SpecialArticleVo();	         
	          vo.setId(id);
	          
	          BaseResult baseResult =specialArticleService.selectById(vo);	          
	          SpecialArticleVo curVo=(SpecialArticleVo) baseResult.getData();
	          if(curVo.getId().equals(id)&&curVo.getTitle().equals(title))
	          {return flag;}
	          if("".equalsIgnoreCase(title)||title==null)
	          {return flag;}
	          flag = specialArticleService.checkTitle(title);         
	          logger.info("checkTitleEdit end.");
	          
	          logger.info("SpecialArticleRestController checkTitleEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("SpecialArticleRestController checkTitleEdit error:" + e.getMessage());
	      logger.info("SpecialArticleRestController checkTitleEdit end.");
	      return flag;
	    }

	  }
	  
	
	

}
